package goldeye

import (
	"fmt"
	"net/http"

	"goweb/ext/app"
	"goweb/ext/e"
	"goweb/service/goldeye"
	"goweb/utils/commons"

	"github.com/gin-gonic/gin"
)

func GetFeedBack(c *gin.Context) {

	startTime := c.Query("startTime")
	endTime := c.Query("endTime")
	title := c.Query("title")
	source := c.Query("source")
	pageSize := c.Query("pageSize")
	pageNo := c.Query("pageNo")
	code := e.SUCCESS

	m := make(map[string]interface{})
	var appG = app.Gin{C: c}
	size := commons.IsAtoi(pageSize, 50)
	if size == -1 {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, "")
		return
	}

	no := commons.IsAtoi(pageNo, 50)
	if no == -1 {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, "")
		return
	}
	if commons.IsYMDTime(startTime) && commons.IsYMDTime(endTime) {
		m["startTime"] = startTime
		m["endTime"] = endTime
	}

	if commons.IsUserLen(title, 1) {
		m["title"] = title
	}

	if commons.IsUserLen(source, 3) {
		m["source"] = source
	}
	fbs := goldeye.FeedBackS{}
	b, count, res := fbs.GetFeedBack(no, size, m)
	data := make(map[string]interface{})
	if b {
		data["count"] = count
		data["data"] = res
	}

	appG.Response(http.StatusOK, code, data)

}

func UpdateFeedBack(c *gin.Context) {
	var appG = app.Gin{C: c}

	backS := goldeye.FeedBackS{}
	code := e.ERROR
	if err := c.ShouldBindJSON(&backS); err == nil {
		fmt.Println()
		if backS.UpdateFeedBack() {
			code = e.SUCCESS
		}
	}
	appG.Response(http.StatusOK, code, nil)
}
//
//func Test(c *gin.Context) {
//	t := model.T{}
//	var appG = app.Gin{C: c}
//	if err := c.ShouldBindJSON(&t); err == nil {
//		test :=Test(t)
//
//		appG.Response(200, 200, test)
//		return
//	} else {
//		fmt.Println(err)
//	}
//
//}
